package tmt.usercenter.web.controller.validtor;

import com.tmt.helper.ReflectHelper;
import com.tmt.spring.validation.TmtCommonValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import tmt.usercenter.web.domain.TmtUser;
import tmt.usercenter.web.domain.UserInfoConfig;
import tmt.usercenter.web.service.TmtUserService;
import tmt.usercenter.web.service.UserInfoConfigService;
import tmt.usercenter.web.vo.user.UserInfoVo;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

/**
 * 用于效验编辑用户信息后提交的数据
 */
@Component
public class UserInfoValidator extends TmtCommonValidator<UserInfoVo> {

    private static final Logger logger = LoggerFactory.getLogger(UserInfoValidator.class);

    @Autowired
    private TmtUserService userService;

    @Autowired
    private UserInfoConfigService userInfoConfigService;

    @Override
    public boolean supports(Class<?> clazz) {
        return UserInfoVo.class.equals(clazz);
    }

    @Override
    public String selectMethodName(UserInfoVo obj, Field field, String methodName) {
        String result = null;
        if (Objects.equals(field.getName(), "idCardNum")) {
            switch (obj.getCardType()) {
                case ID_CARD_HK:
                    result = "validateIdCard_HK";
                    break;
                case ID_CARD_AM:
                    result = "validateIdCard_AM";
                    break;
                case ID_CARD_TW:
                    result = "validateIdCard_TW";
                    break;
                default:
                    result = "validateIdCard2";
                    break;
            }
        }
        return result;
    }

    @Override
    public void validate(Object target, Errors errors) {
        super.validate(target, errors);

        //判断禁止修改的字段是否发生了变动。
        UserInfoVo userInfoVo = (UserInfoVo) target;
        TmtUser user = userService.loadEntity(TmtUser.class, "findByUsername", new Object[]{userInfoVo.getUsername()}, "1");
        if (user != null) {
            UserInfoConfig userInfoConfig = userInfoConfigService.findByOriginSystemClientId(user.getOriginSystem().getClientId(), false);
            if (userInfoConfig != null) {
                List<String> fieldNames = userInfoConfig.getDisableUpdate();
                fieldNames.stream().forEach(x -> {
                    Field field = ReflectHelper.findFieldByName(UserInfoVo.class, x);
                    if (field != null) {
                        Object oldValue = ReflectHelper.getFieldValue(user, x);
                        Object newValue = ReflectHelper.getFieldValue(userInfoVo, x);
                        if (!oldValue.equals(newValue)) {
                            errors.rejectValue(x, null, "字段值禁止修改");
                        }
                    }
                });
            }
        }
    }

    @Override
    public boolean isRepeated(Object obj, String fieldName, Object fieldValue, boolean repeatable) {
        if (repeatable)
            return false;

        boolean isRep = false;
        switch (fieldName) {
            case "username":
                isRep = userService.existsByUsername((String) fieldValue);
                break;
        }
        return isRep;
    }
}